package dolf.game;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.Color;
import dolf.objects.BloodObject;
import dolf.objects.Point;

public class BloodManager implements java.io.Serializable{
	private static final long serialVersionUID = 1L;
	private static BloodManager instance = new BloodManager();
	transient private BufferedImage dryBlood;
	private boolean bloodyGame;
	private boolean bloodyStage;
	private boolean stageClean;
	private double bloodAmount;
	
	/* singleton */

	private BloodManager(){
		int mask = BufferedImage.TYPE_3BYTE_BGR| BufferedImage.TYPE_INT_ARGB; 
	    dryBlood = new BufferedImage (570, 500,mask);
	    this.stageClean = true;
	}

	public static BloodManager getInstance(){
 		return instance;
	}
	
	public void flush(){
		Level.getInstance().removeAllBlood();
		int mask = BufferedImage.TYPE_3BYTE_BGR| BufferedImage.TYPE_INT_ARGB; 
	    dryBlood = new BufferedImage (570, 500,mask);
	    this.stageClean = true;
		
	}
	
	public void removeBlood(BloodObject obj){
		Graphics2D gr = (Graphics2D) this.dryBlood.getGraphics();
		gr.setColor(Color.red);
		for(int i = 0; i < obj.getPositions().length; i++){
			gr.setColor(obj.getColor()[i]);
			gr.fillOval(obj.getPositions()[i].getIntX()-1, obj.getPositions()[i].getIntY()-1, 3, 3);
		}
		if(Level.getInstance().removeBlood(obj) && this.bloodyStage){
			this.stageClean = false;
		}
	}
	
	public void addBlood(int amount, Point position){
		if(this.bloodyGame) {
			Level.getInstance().registerBlood(new BloodObject((int)(amount*this.bloodAmount*2), position));
		}
	}
	
	public void drawDryBlood(Graphics2D g){
		if(!this.stageClean){
			g.drawImage(dryBlood,0,0,null);
		}
	}
	public void changeBloodyGame(double val){
		this.bloodAmount = val;
		if(val == 0){
			this.bloodyGame = false;
			Level.getInstance().removeAllBlood();
			this.bloodyStage = false;
			this.stageClean = true;
		} else {
			this.bloodyGame = true;
			this.bloodyStage = true;
		}
	}
	public void changeBloodyStage(boolean val){
		this.bloodyStage = val;
		if(!val){
			this.stageClean = true;
		}
	}
	public boolean isBloodActive(){
		return this.bloodyGame;
	}
	
	public BufferedImage getDryBlood(){
		return dryBlood;
	}
	
}
